<?php
if (!function_exists('explode_filter_pop')) {
    function explode_filter_pop($str, $delimiter='-') {
        $arr = explode($delimiter, $str);
        $arr = array_filter($arr);
        array_pop($arr);

        return $arr;
    }
}

if (!function_exists('build_tree')) {
    function build_tree($data, $list, $pid = 0)
    {
        foreach ($list as $key => $v) {
            if ($v['parent_id'] == $pid) {
                $row = [
                    'id' => $v['id'],
                    'label' => $v['title'],
                    'children' => []
                ];

                $children = build_tree($row['children'], $list, $v['id']);
                if($children) {
                    $row['children'] = $children;
                } else {
                    unset($row['children']);
                }

                array_push($data, $row);
            }
        }

        return $data;
    }
}

if (!function_exists('build_menu')) {
    function build_menu($data, $list, $pid = 0)
    {
        foreach ($list as $key => $v) {
            if ($v['parent_id'] == $pid) {
                $row = [
                    'id' => $v['id'],
                    'path' => $v['path'],
                    'meta' => [
                        'icon' => $v['icon'],
                        'status' => $v['status'] > 0 ? true : false,
                        'title' => $v['title'],
                        'permissions' => ['download']
                    ],
                    'children' => []
                ];
                $children = build_menu($row['children'], $list, $v['id']);
                if($children) {
                    $row['children'] = $children;
                } else {
                    unset($row['children']);
                }
                array_push($data, $row);
            }
        }

        return $data;
    }
}

if (!function_exists('format_date_range')) {
    function format_date_range($range)
    {
        switch ($range) {
            case is_array($range):
                $start = is_string($range[0]) ? strtotime($range[0]) : $range[0];
                $end = is_string($range[1]) ? strtotime($range[1]) : $range[1];
                $end = $end + 86399;
                break;

            case strpos($range, '~'):
                $arr = explode('~', $range);
                $start = $arr[0];
                $end = $arr[1];
                break;

            case 'today':
                $start = strtotime('today');
                $end = strtotime('tomorrow') - 1;
                break;

            case 'week':
                $start = strtotime('last sunday') + 86400;
                $end = strtotime('tomorrow') - 1;
                break;
            case 'month':
                $start = strtotime(date('Y-m-01'));
                $end = strtotime('tomorrow') - 1;
                break;

            default:
                $start = 0;
                $end = strtotime('tomorrow') - 1;

        }

        return [$start, $end];
    }
}

if (!function_exists('empty_list')) {
    function empty_list($list, $fields) {
        $isEmpty = true;
        foreach ($list as $key=>$row) {
            if (in_array($key, $fields)) {
                if (!empty($row)) {
                    $isEmpty = false;
                    break;
                }
            }
        }

        return $isEmpty;
    }
}

